
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.base.wf.AbstractWorkflow;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.IWorkFlowBO;
import com.wyzz.hr.dao.IWorkFlowApproverDAO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.WorkFlowApprover;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.ObjectUtil;
import com.wyzz.hr.util.StringUtil;

import java.math.BigDecimal;
import java.util.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.ObjectUtils;

public class WorkFlowBOImpl implements IWorkFlowBO {

	public WorkFlowBOImpl() {
	}

	public boolean getNextApproverOfMgr(AbstractWorkflow wf) {
		Employee sup = wf.getOperator();
		BigDecimal amount = wf.getAmount();
		String flowType = wf.getFlowType();
		WorkFlowApprover wfapprover = getWfApproverById(sup.getPosition()
				.getId(), flowType);
		if (wfapprover == null)
			return false;
		if (wf.getOperate().contains("reject"))
			wf.setStatus(Integer.valueOf(21));
		else
			wf.setStatus(Integer.valueOf(5));
		BigDecimal limitAmount = wfapprover.getWorkFlowLimit();
		if (wfapprover.getWorkFlowApproverInd().intValue() == 1
				&& wfapprover.getWorkFlowLimit().doubleValue() == 0.0D) {
			wf.setNextApprover(null);
			wf.setNextApproverDesc("HR");
			return true;
		}
		if (wfapprover.getWorkFlowApproverInd().intValue() == 1
				&& wfapprover.getWorkFlowLimit().doubleValue() > 0.0D
				&& limitAmount.compareTo(amount) >= 0) {
			wf.setNextApprover(null);
			wf.setNextApproverDesc("HR");
			return true;
		}
		wfapprover = getWfApprover_tree(sup, null, flowType);
		if (wfapprover != null) {
			wf.setNextApprover(wfapprover.getWorkFlowApproverId());
			wf.setNextApproverDesc(wfapprover.getNextApproverDesc());
			return true;
		} else {
			wf.setNextApprover(null);
			wf.setNextApproverDesc("HR");
			return true;
		}
	}

	public boolean getNextApproverOfOwn(AbstractWorkflow wf) {
		String flowType = wf.getFlowType();
		wf.setStatus(Integer.valueOf(2));
		WorkFlowApprover wfapprover = getWfApprover_tree(wf.getApplier(), wf
				.getApplyDeptNo(), flowType);
		if (wfapprover != null) {
			wf.setNextApprover(wfapprover.getWorkFlowApproverId());
			wf.setNextApproverDesc(wfapprover.getNextApproverDesc());
			return true;
		} else {
			wf.setNextApprover(null);
			wf.setNextApproverDesc("HR");
			return false;
		}
	}

	private WorkFlowApprover getWfApprover_tree(Employee emp, Department dept,
			String flowType) {
		String wfAppDesc = "{0}({1})";
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		Position supPosArr[];
		if (emp == null)
			supPosArr = posBo.getAllSupPosIds(1, dept);
		else if (emp.getPosition() != null)
			supPosArr = posBo.getAllSupPosIds(0, emp.getPosition().getId());
		else
			supPosArr = posBo.getAllSupPosIds(0, emp);
		if (ObjectUtils.isEmpty(supPosArr))
			return null;
		List supWFAppList = getWfApproverByIds(ObjectUtil.convObjToIdArr(
				supPosArr, new String[0]), flowType);
		if (supWFAppList == null || supWFAppList.size() == 0)
			return null;
		Map supWFAppMap = new HashMap();
		WorkFlowApprover wfapprover;
		for (Iterator i$ = supWFAppList.iterator(); i$.hasNext(); supWFAppMap
				.put(wfapprover.getWorkFlowApproverId(), wfapprover))
			wfapprover = (WorkFlowApprover) i$.next();

		wfapprover = null;
		Position arr$[] = supPosArr;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Position pos = arr$[i$];
			wfapprover = (WorkFlowApprover) supWFAppMap.get(pos.getId());
			if (pos.getPositionEmpNo() != null && wfapprover != null) {
				String empName = pos.getPositionEmpNo().getEmpName();
				wfapprover.setNextApproverDesc(StringUtil.message(wfAppDesc,
						new Object[] { pos.getPositionPbId().getPbName(),
								empName }));
				return wfapprover;
			}
		}

		return null;
	}

	public IWorkFlowApproverDAO getWfdao() {
		return wfdao;
	}

	public void setWfdao(IWorkFlowApproverDAO wfdao) {
		this.wfdao = wfdao;
	}

	public WorkFlowApprover getWfApproverById(String approveId, String flowType) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.WorkFlowApprover.class);
		dc.add(Restrictions.eq("workFlowApproverId", approveId));
		dc.add(Restrictions.eq("workFlowApproverType", flowType));
		List list = wfdao.findByCriteria(dc);
		return list == null || list.size() <= 0 ? null
				: (WorkFlowApprover) list.get(0);
	}

	public List getWfApproverByIds(String approveIds[], String flowType) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.WorkFlowApprover.class);
		dc.add(Restrictions.in("workFlowApproverId", approveIds));
		dc.add(Restrictions.eq("workFlowApproverType", flowType));
		return wfdao.findByCriteria(dc);
	}

	public String checkEmpCharge(String deptLocsInCharge[][], Employee emp) {
		String deptsInCharge[] = deptLocsInCharge[0];
		String locsInCharge[] = deptLocsInCharge[1];
		if (deptsInCharge != null
				&& deptsInCharge.length > 0
				&& ObjectUtil.contains(deptsInCharge, emp.getEmpDeptNo()
						.getId()))
			return "DEPTINCHARGE";
		if (locsInCharge != null
				&& locsInCharge.length > 0
				&& ObjectUtil.contains(locsInCharge, emp.getEmpLocationNo()
						.getId()))
			return "LOCINCHARGE";
		else
			return "NOCHARGE";
	}

	public IWorkFlowApproverDAO wfdao;
}
